package boxuegu.com.boxuegu2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import boxuegu.com.boxuegu2.utils.MD5Utils;
import boxuegu.com.boxuegu2.utils.SPreadWrite;
import boxuegu.com.boxuegu2.view.TitleBar;

public class LoginActivity extends AppCompatActivity {

    private TextView tv_register,tv_find_psw;
    private Button btn_login;
    private EditText et_user_name,et_psw;
    private LinearLayout title_bar;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //当注册界面回跳回来自动执行下面代码p71:116
        //将携带回来的数据显示到登录的文本框
        if(resultCode==999){//这里检查从哪个地方跳回来的
            if(data!=null) {
                et_user_name.setText(data.getStringExtra("userName"));
                et_psw.setText(data.getStringExtra("psw"));
            }
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=et_user_name.getText().toString();
                String psw=et_psw.getText().toString();
                if(userName.equals("")){//判断用户名为空
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_LONG).show();
                    return;
                }
                if(psw.equals("")){//判断密码为空
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                    return;
                }
                //接下来判断用户名和密码是否正确
                String md5psw= MD5Utils.md5(psw);
                String spPsw;
                SharedPreferences sharedPreferences=getSharedPreferences("loginInfo",MODE_PRIVATE);
                spPsw=sharedPreferences.getString(userName,"");

                if(spPsw==null||spPsw.length()==0){
                    Toast.makeText(LoginActivity.this,"该用户不存在",Toast.LENGTH_LONG).show();
                    return;
                }
                if(!spPsw.equals(md5psw)){
                    Toast.makeText(LoginActivity.this,"输入的密码不正确",Toast.LENGTH_LONG).show();
                    return;
                }
                //登录成功p70：78
                //提示，保存登录状态到sp文件中
                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                SPreadWrite.saveLoginStatus(LoginActivity.this,userName);

                //回跳到主界面,带过去用户名p70:82
                Intent data=new Intent();
                data.putExtra("isLogin",true);
                data.putExtra("userName",userName);
                setResult(111,data);//这里就是回跳，回跳到主界面activity，不是MyInfoView，带过去数据data，
                // 回自动执行主界面activity的一个回调方法
                LoginActivity.this.finish();
            }
        });


        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到找回密码界面
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册界面p69:49
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,888);
            }
        });
    }

    private void init(){
        btn_login=findViewById(R.id.btn_login);
        et_user_name=findViewById(R.id.et_user_name);
        et_psw=findViewById(R.id.et_psw);
        title_bar=findViewById(R.id.title_bar);
        TitleBar titleBar=new TitleBar(this,"登录");
        title_bar.addView(titleBar.getView());
        tv_register=findViewById(R.id.tv_register);
        tv_find_psw=findViewById(R.id.tv_find_psw);
    }
}
